# remove existing objects
rm(list = ls())
# download RData file into your working directory
rdata <- "https://github.com/ucb-stat133/stat133-labs/raw/master/data/nba2018-salary-points.RData"

download.file(url = rdata, destfile = 'nba2018-salary-points.RData')
# load data in your R session
load('nba2018-salary-points.RData')
# list the available objects with ls()
ls()
##  [1] "age"        "experience" "player"     "points1"    "points2"   
##  [6] "points3"    "position"   "rdata"      "salary"     "scored"    
## [11] "team"
four <- head(player, n = 4)
# number one#
four[1]
## [1] "Al Horford"
# an index of zero:# 
four[0]
## character(0)
#a negative index: #
four[-1]
## [1] "Amir Johnson"      "Avery Bradley"     "Demetrius Jackson"
#various negative indices: #
four[-c(1,2,3)]
## [1] "Demetrius Jackson"
#an index greater than the length of the vector: #
four[5]
## [1] NA
#repeated indices: #
four[c(1,2,2,3,3,3)]
## [1] "Al Horford"    "Amir Johnson"  "Amir Johnson"  "Avery Bradley"
## [5] "Avery Bradley" "Avery Bradley"

Figure out how to use seq(), rep(), and bracket notation, to extract:

all the even elements in player
all the odd elements in salary
all multiples of 5 (e.g. 5, 10, 15, etc) of team
elements in positions 10, 20, 30, 40, etc of scored
all the even elements in team but this time in reverse order
player[seq(2, length(player), by = 2)]
##   [1] "Amir Johnson"             "Demetrius Jackson"       
##   [3] "Isaiah Thomas"            "James Young"             
##   [5] "Jonas Jerebko"            "Kelly Olynyk"            
##   [7] "Terry Rozier"             "Andrew Bogut"            
##   [9] "Dahntay Jones"            "Derrick Williams"        
##  [11] "Iman Shumpert"            "James Jones"             
##  [13] "Kay Felder"               "Kyle Korver"             
##  [15] "Larry Sanders"            "Richard Jefferson"       
##  [17] "Bruno Caboclo"            "Delon Wright"            
##  [19] "DeMarre Carroll"          "Jakob Poeltl"            
##  [21] "Kyle Lowry"               "Norman Powell"           
##  [23] "Pascal Siakam"            "Serge Ibaka"             
##  [25] "Bradley Beal"             "Chris McCullough"        
##  [27] "Danuel House"             "Jason Smith"             
##  [29] "Kelly Oubre"              "Markieff Morris"         
##  [31] "Tomas Satoransky"         "DeAndre' Bembry"         
##  [33] "Dwight Howard"            "Gary Neal"               
##  [35] "Kent Bazemore"            "Lamar Patterson"         
##  [37] "Mike Dunleavy"            "Paul Millsap"            
##  [39] "Taurean Waller-Prince"    "Tim Hardaway"            
##  [41] "Gary Payton"              "Greg Monroe"             
##  [43] "Jason Terry"              "Khris Middleton"         
##  [45] "Matthew Dellavedova"      "Miles Plumlee"           
##  [47] "Rashad Vaughn"            "Steve Novak"             
##  [49] "Thon Maker"               "Aaron Brooks"            
##  [51] "C.J. Miles"               "Glenn Robinson"          
##  [53] "Joe Young"                "Lance Stephenson"        
##  [55] "Monta Ellis"              "Paul George"             
##  [57] "Rodney Stuckey"           "Anthony Morrow"          
##  [59] "Cameron Payne"            "Denzel Valentine"        
##  [61] "Isaiah Canaan"            "Jimmy Butler"            
##  [63] "Michael Carter-Williams"  "Paul Zipser"             
##  [65] "Rajon Rondo"              "Dion Waiters"            
##  [67] "Hassan Whiteside"         "Josh McRoberts"          
##  [69] "Justise Winslow"          "Okaro White"             
##  [71] "Tyler Johnson"            "Wayne Ellington"         
##  [73] "Andre Drummond"           "Beno Udrih"              
##  [75] "Darrun Hilliard"          "Ish Smith"               
##  [77] "Kentavious Caldwell-Pope" "Michael Gbinije"         
##  [79] "Reggie Jackson"           "Tobias Harris"           
##  [81] "Brian Roberts"            "Christian Wood"          
##  [83] "Frank Kaminsky"           "Johnny O'Bryant"         
##  [85] "Marco Belinelli"          "Michael Kidd-Gilchrist"  
##  [87] "Nicolas Batum"            "Treveon Graham"          
##  [89] "Chasson Randle"           "Derrick Rose"            
##  [91] "Justin Holiday"           "Kyle O'Quinn"            
##  [93] "Marshall Plumlee"         "Mindaugas Kuzminskas"    
##  [95] "Sasha Vujacic"            "Aaron Gordon"            
##  [97] "Arinze Onuaku"            "C.J. Watson"             
##  [99] "D.J. Augustin"            "Elfrid Payton"           
## [101] "Jeff Green"               "Marcus Georges-Hunt"     
## [103] "Nikola Vucevic"           "Stephen Zimmerman"       
## [105] "Alex Poythress"           "Gerald Henderson"        
## [107] "Jahlil Okafor"            "Joel Embiid"             
## [109] "Justin Harper"            "Richaun Holmes"          
## [111] "Sergio Rodriguez"         "T.J. McConnell"          
## [113] "Timothe Luwawu-Cabarrot"  "Anthony Bennett"         
## [115] "Brook Lopez"              "Greivis Vasquez"         
## [117] "Jeremy Lin"               "Justin Hamilton"         
## [119] "Luis Scola"               "Randy Foye"              
## [121] "Sean Kilpatrick"          "Trevor Booker"           
## [123] "Anderson Varejao"         "Damian Jones"            
## [125] "Draymond Green"           "James Michael McAdoo"    
## [127] "Kevin Durant"             "Klay Thompson"           
## [129] "Patrick McCaw"            "Stephen Curry"           
## [131] "Bryn Forbes"              "David Lee"               
## [133] "Dejounte Murray"          "Joel Anthony"            
## [135] "Kawhi Leonard"            "LaMarcus Aldridge"       
## [137] "Nicolas Laprovittola"     "Pau Gasol"               
## [139] "Bobby Brown"              "Clint Capela"            
## [141] "Isaiah Taylor"            "Lou Williams"            
## [143] "Nene Hilario"             "Ryan Anderson"           
## [145] "Trevor Ariza"             "Alan Anderson"           
## [147] "Blake Griffin"            "Brice Johnson"           
## [149] "DeAndre Jordan"           "J.J. Redick"             
## [151] "Luc Mbah a Moute"         "Paul Pierce"             
## [153] "Wesley Johnson"           "Boris Diaw"              
## [155] "Derrick Favors"           "Gordon Hayward"          
## [157] "Joe Ingles"               "Joel Bolomboy"           
## [159] "Rodney Hood"              "Shelvin Mack"            
## [161] "Alex Abrines"             "Domantas Sabonis"        
## [163] "Enes Kanter"              "Josh Huestis"            
## [165] "Nick Collison"            "Russell Westbrook"       
## [167] "Steven Adams"             "Victor Oladipo"          
## [169] "Brandan Wright"           "Deyonta Davis"           
## [171] "JaMychal Green"           "Marc Gasol"              
## [173] "Toney Douglas"            "Troy Daniels"            
## [175] "Wade Baldwin"             "Zach Randolph"           
## [177] "Allen Crabbe"             "Damian Lillard"          
## [179] "Evan Turner"              "Jusuf Nurkic"            
## [181] "Meyers Leonard"           "Pat Connaughton"         
## [183] "Alonzo Gee"               "Darrell Arthur"          
## [185] "Gary Harris"              "Jameer Nelson"           
## [187] "Juan Hernangomez"         "Malik Beasley"           
## [189] "Mike Miller"              "Roy Hibbert"             
## [191] "Wilson Chandler"          "Anthony Davis"           
## [193] "Dante Cunningham"         "Donatas Motiejunas"      
## [195] "Jarrett Jack"             "Jrue Holiday"            
## [197] "Omri Casspi"              "Reggie Williams"         
## [199] "Tim Frazier"              "Ben Bentil"              
## [201] "Devin Harris"             "Dorian Finney-Smith"     
## [203] "Harrison Barnes"          "Jarrod Uthoff"           
## [205] "Manny Harris"             "Nicolas Brussino"        
## [207] "Salah Mejri"              "Wesley Matthews"         
## [209] "Arron Afflalo"            "Buddy Hield"             
## [211] "Garrett Temple"           "Jordan Farmar"           
## [213] "Langston Galloway"        "Rudy Gay"                
## [215] "Ty Lawson"                "Willie Cauley-Stein"     
## [217] "Andrew Wiggins"           "Cole Aldrich"            
## [219] "John Lucas III"           "Karl-Anthony Towns"      
## [221] "Nemanja Bjelica"          "Shabazz Muhammad"        
## [223] "Zach LaVine"              "Corey Brewer"            
## [225] "David Nwaba"              "Jordan Clarkson"         
## [227] "Luol Deng"                "Nick Young"              
## [229] "Thomas Robinson"          "Tyler Ennis"             
## [231] "Alex Len"                 "Derrick Jones"           
## [233] "Dragan Bender"            "Eric Bledsoe"            
## [235] "Jarell Eddie"             "Leandro Barbosa"         
## [237] "Ronnie Price"             "Tyler Ulis"
salary[seq(1, length(player), by = 2)]
##   [1] 26540100  8269663  1410598  6286408  4743000  1223653  3578880
##   [8]  8000000  7806971   259626     5145 12800000   874636 21165675
##  [15] 17638063 30963450 15330435  7330000 26540100   543471 14382022
##  [22]  1921320  5300000  6050000  3730653  1200000   543471 15944154
##  [29] 16957900 12000000  5893981  3386598  2708582  8400000   392478
##  [36]  4000000  2500000  1015696   418228  3850000    51449  2995421
##  [43]  5374320 12517606   925000  1403611 10500000  6348759   236457
##  [50]  2368327 10230179   650000  8800000  1800000  4000000  2463840
##  [57]  1052342 14153652  1453680   874636 23200000  1643040  1709720
##  [64]  5782450   425000 13219250 15890000  4000000   874636  1227000
##  [71]   543471  4000000  1015696  6500000  7000000  1704120 10991957
##  [78]  4625000  2255644  2969880   375579   231521  5318313  6511628
##  [85] 12000000 12250000   138938  6000000 24559380 11242000 17000000
##  [92]  4317720  6191000   543471   543471  1375000    51449 17000000
##  [99]  1209680   980431 17000000  6540000  3909840    31969 10000000
## [106]  2318280   442126  9424084  1514160  2993040  1015696    96969
## [113]  8550000  6088993   194494  1562280  1074145   980431  3333333
## [120]  1914544  1395600   726672   202300 11131368  1551659  1015696
## [127]  1403611  1182840   383351  5782450  2898000 10000000   543471
## [134]  2898000   874636  1192080 14000000  3578948 14445313   543471
## [141] 12385364 26540100  1045000  6000000  1720560   181969 11000000
## [148]  1551659 22868827   543471 13253012  1403611  1551659 10154495
## [155]  3940320  8000000  1015696 11000000   937800  2121288  2340600
## [162]  2183072  2483040   980431  4837500   247991   543471  8950000
## [169]   945000 22116750  2898000  1286160 26540100  5505618  4264057
## [176]    83119  7680965  3219579  6666667   600000  8988764  2751360
## [183]  1350120 15050000  3241800  3210840   150000 12078652  2328530
## [190]  1358500  3533333  4600000   543471 16957900  8081363   234915
## [197]  9904494    79922 11241218   650000  1015696 25000000  8375000
## [204]  4096950   680937  4384490   105498  2898000  8000000  4008882
## [211]  5229454  2202240  8046500  1439880  1188840 10661286  2022240
## [218]  3500000  2348783  3911380  3872520 13550000  1339680  5281680
## [225]  5332800  1034956  3267120  1551659  6191000 16000000   874636
## [232] 12606250  2223600    23069 10470000   469841  2941440  2128920
## [239] 12415000
team[seq(5, length(player), by = 5)]
##  [1] BOS BOS BOS CLE CLE CLE TOR TOR TOR WAS WAS WAS ATL ATL ATL ATL MIL
## [18] MIL MIL IND IND IND IND CHI CHI CHI MIA MIA MIA DET DET DET CHO CHO
## [35] CHO NYK NYK NYK ORL ORL ORL PHI PHI PHI PHI BRK BRK BRK BRK GSW GSW
## [52] GSW SAS SAS SAS HOU HOU HOU LAC LAC LAC UTA UTA UTA OKC OKC OKC MEM
## [69] MEM MEM POR POR POR DEN DEN DEN NOP NOP NOP DAL DAL DAL DAL SAC SAC
## [86] SAC MIN MIN MIN LAL LAL LAL PHO PHO PHO
## 30 Levels: ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL ... WAS
scored[seq(10, length(player), by = 10)]
##  [1]  299  156    4  165 1779 1063  801 1143  577  322  630  291  538   98
## [15]  127 1321  780 1154  124 1046   64  559 1539  357  776 1999 1888  818
## [29]  936 1173  476  430   33   58  106  304  851    4    6    0    8  378
## [43]  681 2061  120  170   24
rev(team[seq(2, length(player), by = 2)])
##   [1] PHO PHO PHO PHO PHO PHO PHO PHO LAL LAL LAL LAL LAL LAL LAL MIN MIN
##  [18] MIN MIN MIN MIN MIN SAC SAC SAC SAC SAC SAC SAC SAC DAL DAL DAL DAL
##  [35] DAL DAL DAL DAL DAL NOP NOP NOP NOP NOP NOP NOP NOP DEN DEN DEN DEN
##  [52] DEN DEN DEN DEN DEN POR POR POR POR POR POR MEM MEM MEM MEM MEM MEM
##  [69] MEM MEM OKC OKC OKC OKC OKC OKC OKC OKC UTA UTA UTA UTA UTA UTA UTA
##  [86] LAC LAC LAC LAC LAC LAC LAC LAC HOU HOU HOU HOU HOU HOU HOU SAS SAS
## [103] SAS SAS SAS SAS SAS SAS GSW GSW GSW GSW GSW GSW GSW GSW BRK BRK BRK
## [120] BRK BRK BRK BRK BRK BRK PHI PHI PHI PHI PHI PHI PHI PHI PHI ORL ORL
## [137] ORL ORL ORL ORL ORL ORL ORL NYK NYK NYK NYK NYK NYK NYK CHO CHO CHO
## [154] CHO CHO CHO CHO CHO DET DET DET DET DET DET DET DET MIA MIA MIA MIA
## [171] MIA MIA MIA CHI CHI CHI CHI CHI CHI CHI CHI IND IND IND IND IND IND
## [188] IND IND MIL MIL MIL MIL MIL MIL MIL MIL MIL ATL ATL ATL ATL ATL ATL
## [205] ATL ATL ATL WAS WAS WAS WAS WAS WAS WAS TOR TOR TOR TOR TOR TOR TOR
## [222] TOR CLE CLE CLE CLE CLE CLE CLE CLE CLE BOS BOS BOS BOS BOS BOS BOS
## 30 Levels: ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL ... WAS

Logical Subsetting and Comparisons

scored_four <- scored[1:4]

# elements greater than 100
scored_four[scored_four > 100]
## [1] 952 520 894
# elements less than 100
scored_four[scored_four < 100]
## [1] 10
# elements less than or equal to 10
scored_four[scored_four <= 10]
## [1] 10
# elements different from 10
scored_four[scored_four != 10]
## [1] 952 520 894
TRUE & TRUE
## [1] TRUE
TRUE & FALSE
## [1] FALSE
FALSE & FALSE
## [1] FALSE
# OR
TRUE | TRUE
## [1] TRUE
TRUE | FALSE
## [1] TRUE
FALSE | FALSE
## [1] FALSE
# NOT
!TRUE
## [1] FALSE
!FALSE
## [1] TRUE
# players of Golden State (GSW)
player[team == 'GSW']
##  [1] "Anderson Varejao"     "Andre Iguodala"       "Damian Jones"        
##  [4] "David West"           "Draymond Green"       "Ian Clark"           
##  [7] "James Michael McAdoo" "JaVale McGee"         "Kevin Durant"        
## [10] "Kevon Looney"         "Klay Thompson"        "Matt Barnes"         
## [13] "Patrick McCaw"        "Shaun Livingston"     "Stephen Curry"       
## [16] "Zaza Pachulia"
# name of players with salaries greater than 20 million dollars
player[salary > 20000000]
##  [1] "Al Horford"        "Kevin Love"        "LeBron James"     
##  [4] "DeMar DeRozan"     "Bradley Beal"      "Dwight Howard"    
##  [7] "Paul Millsap"      "Dwyane Wade"       "Hassan Whiteside" 
## [10] "Andre Drummond"    "Nicolas Batum"     "Carmelo Anthony"  
## [13] "Derrick Rose"      "Brook Lopez"       "Kevin Durant"     
## [16] "LaMarcus Aldridge" "James Harden"      "Blake Griffin"    
## [19] "Chris Paul"        "DeAndre Jordan"    "Russell Westbrook"
## [22] "Chandler Parsons"  "Marc Gasol"        "Mike Conley"      
## [25] "Damian Lillard"    "Anthony Davis"     "Dirk Nowitzki"    
## [28] "Harrison Barnes"
# name of players with scored points between 1000 and 1200 (exclusive)
player[scored > 1000 & scored < 1200]
##  [1] "Kevin Love"               "Markieff Morris"         
##  [3] "Otto Porter"              "Dwight Howard"           
##  [5] "Tim Hardaway"             "Jabari Parker"           
##  [7] "Myles Turner"             "Dwyane Wade"             
##  [9] "Tyler Johnson"            "Andre Drummond"          
## [11] "Kentavious Caldwell-Pope" "Marcus Morris"           
## [13] "Nicolas Batum"            "Derrick Rose"            
## [15] "Kristaps Porzingis"       "Aaron Gordon"            
## [17] "Elfrid Payton"            "Evan Fournier"           
## [19] "Nikola Vucevic"           "Dario Saric"             
## [21] "Chris Paul"               "DeAndre Jordan"          
## [23] "J.J. Redick"              "Jamal Crawford"          
## [25] "Rudy Gobert"              "Enes Kanter"             
## [27] "Victor Oladipo"           "Zach Randolph"           
## [29] "Danilo Gallinari"         "Wilson Chandler"         
## [31] "Jrue Holiday"

Write commands, using bracket notation, to answer the following questions (you may need to use min(), max(), which(), which.min(), which.max()):

#players in position Center, of Warriors (GSW)

player[position == "C" & team == "GSW"]
## [1] "Anderson Varejao" "Damian Jones"     "David West"      
## [4] "JaVale McGee"     "Kevon Looney"     "Zaza Pachulia"
#players of both GSW (warriors) and LAL (lakers)
player[team == 'GSW' | team == 'LAL']
##  [1] "Anderson Varejao"     "Andre Iguodala"       "Damian Jones"        
##  [4] "David West"           "Draymond Green"       "Ian Clark"           
##  [7] "James Michael McAdoo" "JaVale McGee"         "Kevin Durant"        
## [10] "Kevon Looney"         "Klay Thompson"        "Matt Barnes"         
## [13] "Patrick McCaw"        "Shaun Livingston"     "Stephen Curry"       
## [16] "Zaza Pachulia"        "Brandon Ingram"       "Corey Brewer"        
## [19] "D'Angelo Russell"     "David Nwaba"          "Ivica Zubac"         
## [22] "Jordan Clarkson"      "Julius Randle"        "Luol Deng"           
## [25] "Metta World Peace"    "Nick Young"           "Tarik Black"         
## [28] "Thomas Robinson"      "Timofey Mozgov"       "Tyler Ennis"
# players in positions Shooting Guard and Point Guards, of Lakers (LAL)
player[team == 'LAL' & (position == 'SG' | position == 'PG')]
## [1] "D'Angelo Russell" "David Nwaba"      "Jordan Clarkson" 
## [4] "Nick Young"       "Tyler Ennis"
# subset Small Forwards of GSW and LAL

player[position == 'SF' & (team == 'GSW' | team == 'LAL')]
## [1] "Andre Iguodala"    "Matt Barnes"       "Brandon Ingram"   
## [4] "Corey Brewer"      "Luol Deng"         "Metta World Peace"
# name of the player with largest salary
player[which.max(salary)]
## [1] "LeBron James"
# name of the player with smallest salary
player[which.min(salary)]
## [1] "Edy Tavares"
# name of the player with largest number of scored points
player[which.max(scored)]
## [1] "Russell Westbrook"
# salary of the player with largest number of scored points
salary[which.max(scored)]
## [1] 26540100
# largest salary of all Centers
max(salary[position == 'C'])
## [1] 26540100
# team of the player with the largest number of scored points
team[which.max(scored)]
## [1] OKC
## 30 Levels: ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL ... WAS
# name of the player with the largest number of 3-pointers
player[which.max(points3)]
## [1] "Stephen Curry"

Subsetting with Character Vectors

plot(scored, salary)

library(plotly)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.4.4
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot_ly(x = scored, y = salary, type = "scatter", mode = "markers")
## Warning: package 'bindrcpp' was built under R version 3.4.4
log_scored <- log(scored)
log_salary <- log(salary)

plot(log_scored, log_salary)

plot(log_scored, log_salary)
text(log_scored, log_salary, labels = player)

plot(log_scored, log_salary)
text(log_scored, log_salary, labels = abbreviate(player))

# Your Turn: create a scatterplot of points and salary for the Warriors (GSW), displaying the names of the players. Generate two scatterplots, one with raw values (original scale, and another plot with log-transformations).

plot(x = scored[team == "GSW"], y = salary[team == "GSW"])
text(scored[team == "GSW"], salary[team == "GSW"], labels = player[team == "GSW"])

plot(x = log(scored[team == "GSW"]), y = log(salary[team == "GSW"]))
text(log(scored[team == "GSW"]), log(salary[team == "GSW"]), labels = player[team == "GSW"])

Factors

is.factor(team)
## [1] TRUE
position_fac <- factor(position)
table(position_fac)
## position_fac
##   C  PF  PG  SF  SG 
##  97  98  96  84 102
position_fac[1:5]
## [1] C  PF SG PG SF
## Levels: C PF PG SF SG
# positions of Warriors
position_fac[team == 'GSW']
##  [1] C  SF C  C  PF SG PF C  PF C  SG SF SG PG PG C 
## Levels: C PF PG SF SG
# positions of players with salaries > 15 millions
position_fac[salary > 15000000]
##  [1] C  PF PG SF C  SG SG C  PG C  SF PF C  SF SF SG SF PG C  C  PF SG SF
## [24] PG C  C  SG C  PF PF SG SF PF C  PG PF PF PG C  SF C  PG SF C  PG SG
## [47] PG SF SF C  C  PF PF SG SF C 
## Levels: C PF PG SF SG
# frequencies (counts) of positions with salaries > 15 millions: use table
table(position_fac[salary > 15000000])
## 
##  C PF PG SF SG 
## 17 10  9 12  8
# relative frequencies (proportions) of 'SG' (Shooting Guards) in each team
prop.table(table(team[position_fac == 'SG']))
## 
##        ATL        BOS        BRK        CHI        CHO        CLE 
## 0.02941176 0.02941176 0.03921569 0.04901961 0.04901961 0.03921569 
##        DAL        DEN        DET        GSW        HOU        IND 
## 0.03921569 0.03921569 0.02941176 0.02941176 0.02941176 0.01960784 
##        LAC        LAL        MEM        MIA        MIL        MIN 
## 0.02941176 0.02941176 0.02941176 0.03921569 0.03921569 0.01960784 
##        NOP        NYK        OKC        ORL        PHI        PHO 
## 0.01960784 0.03921569 0.01960784 0.04901961 0.02941176 0.04901961 
##        POR        SAC        SAS        TOR        UTA        WAS 
## 0.02941176 0.04901961 0.03921569 0.01960784 0.01960784 0.02941176
plot(scored, salary, col= position_fac, pch = 25, cex = 2, xlab= "Scores", ylab = "Salary")